home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Dr. Windows 3
/
dr win3.zip
/
dr win3
/
WINPROGS
/
IWF12.ZIP
/
KERNEL
/
IMAGEIO.C
< prev
next >
Wrap
C/C++ Source or Header
|
1994-02-19
|
24KB
|
768 lines
/*
-------------------------------------------------------------------------------
IMAGEIO.C (Image Input Output)
CREATOR: Craig Muller P.Eng. 1991,1992,1993
cmuller@ccu.umanitoba.ca
Computer Vision Laboratory
University of Manitoba
Winnipeg, Manitoba. R3T 2N2
CANADA.
DESCRIPTION:
These procedures provide a general purpose interface to the image
data and only have compatibility with the Windows environment.
PROCEDURES:
CreateImage() - Creates an image which can be used for storing pixel data.
DestroyImage() - Frees an image used for storing pixel data.
ClearImage() - Clears an image to zero.
CopyImageData() - Copies the pixel data from source to destination image.
CopyImageRect() - Copies a rectangle of pixels from source to destination image.
CopyImageRow() - Copies a row of pixels from source to destination image.
CopyImagePal() - Copies an image palette to a destination image
DuplicateImage() - Creates a new image which is a duplicate of the original less data
ReduceImage() - Creates a new image which is a reduction of the original.
ScaleImage() - Creates a new image which scales the original into it.
MirrorImage() - Creates a new image which is a mirror of the original
FlipImage() - Creates a new image which is a flip of the original
RotateImage() - Creates a new image which is a rotation of the original
GetImagePixel() - Gets a pixel from an image at x,y and stores it in dn.
SetImagePixel() - Sets a pixel value in the source frame buffer at x,y to a value of dn.
GetImageScan() - Gets a raster line from an image at line y and stores it at dn.
GetImageRow() - Gets a row of pixels from an image and stores it at dn.
PutImageRow() - Puts a row of pixels from a buffer into an image
SetImageRow() - Sets a row of pixels to a value
GetImageCol() - Gets a column of pixels from an image and stores it at dn.
PutImageCol() - Puts a column of pixels from a buffer into an image
SetImageCol() - Sets a column of pixels to a value
GetImageRect() - Cuts a rectangle from an image and stores it in the buffer.
PutImageRect() - Pastes a rectangle from the buffer to an image.
SetImageRect() - Sets a rectangular region of pixels to a value
ImageMessage() - Puts an error message on the screen
-------------------------------------------------------------------------------
*/
#include <windows.h>
#pragma hdrstop
#include <stdio.h>
#include <dos.h>
#include <math.h>
#include <mem.h>
#include <alloc.h>
#include <string.h>
#include "kernel.h"
void CopyImageRow(IMAGE *s_image,IMAGE *d_image,int x,int y,int cb);
void CopyImageRect(IMAGE *s_image,IMAGE *d_image,RECT *rs);
/*
---------------------------------------------------------------------
ImageMessage() - Puts an error message on the screen
~~~~~~~~~~
COMMENTS:
---------------------------------------------------------------------
*/
void ImageMessage(char *sz)
{
MessageBeep(MB_ICONEXCLAMATION);
MessageBox(NULL,sz,"KERNEL ERROR (FBIO)",MB_OK | MB_ICONEXCLAMATION);
}
/*
---------------------------------------------------------------------
IMAGE *CreateImage(int w,int h)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This procedure allows the user to create a memory Frame Buffer (IMAGE)
in the PC memory space. There is no limit on the number of images
that can be created as long as there is memory space available.
Parameters:
w - width of image
h - height of image
Return value:
(IMAGE *) - pointer to the new image structure.
---------------------------------------------------------------------
*/
IMAGE *CreateImage(int w,int h)
{
char sz[80];
int i;
IMAGE *image;
// Allocate local memory space for the data structure for the IMAGE object.
image = (IMAGE *)LocalAlloc(LPTR,sizeof(IMAGE));
if (!image)
{
ImageMessage("Unable to Allocate Local Memory");
return(image);
}
if (w>MAXW)
{
sprintf(sz,"Width exceeds %d pixels, image will be truncated",MAXW);
ImageMessage(sz);
w = MAXW;
}
if (h>MAXH)
{
sprintf(sz,"Height exceeds %d pixels, image will be truncated",MAXW);
ImageMessage(sz);
h = MAXH;
}
strcpy(image->comment,"No Comments"); // Default comment string
strcpy(image->fspec,"Untitled"); // Default file name string
image->color = FALSE; // Image by default is gray
image->hres = w; // Set the image width
image->vres = h; // Set the image height
image->depth = 8; // Only uses 8 bit images
image->palsize= 256; // Default palette entries used
image->gamma = 0.5; // Default gamma value
// *** IMPORTANT ***
// Even though the width of an image can be any value, when the image
// is displayed in Windows the bitmap scan must fit on a 32 bit boundary
// or else the display will appear all garbled. Scansize is used for this.
image->scansize = (w%4) ? (w/4+1)*4 : w; // Must fit 32 bit boundary.
SetRect(&image->rc,0,0,image->hres-1,image->vres-1); // Set default client rectangle
// Since the default image is gray set the input and output LUTS
// to a gray scale.
for (i=0; i<256; ++i)
{
image->ILUT[i] = i;
}
for (i=0; i<256; ++i)
{
image-> Pal[i].peRed = i;
image-> Pal[i].peGreen = i;
image-> Pal[i].peBlue = i;
image-> Pal[i].peFlags = 0;
image-> LUT[i].peRed = i;
image-> LUT[i].peGreen = i;
image-> LUT[i].peBlue = i;
image-> LUT[i].peFlags = 0;
}
// Allocate global memory for holding the image data.
image->hData = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,(DWORD)image->scansize*image->vres);
if (!image->hData)
{
ImageMessage("Unable to Allocate Global Memory");
image = (IMAGE *)LocalFree((LOCALHANDLE)image);
}
return(image); // Return a pointer to the new image
}
/*
---------------------------------------------------------------------
IMAGE *DestroyImage(IMAGE *image)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Destroys a Frame Buffer object by first freeing any data memory and
then freeing the object structure. The object is returned if it cannot
be destroyed for any reason, otherwise NULL is returned if OK.
Parameters:
image - pointer to image to be destroyed
Return value:
(IMAGE *) - null pointer to image if successful.
---------------------------------------------------------------------
*/
IMAGE *DestroyImage(IMAGE *image)
{
if (image) // If the image is valid
{
if (image->hData) // If the data is valid
image->hData = GlobalFree(image->hData); // Free the image data
if (image->hData) // If the data is still valid
{ // It was unable to free it
ImageMessage("Unable to Free Global Memory");
return(image);
}
image = (IMAGE *)LocalFree((LOCALHANDLE)image); // Free the local structure
if (image) // If it is still there
{ // It was unable to free it
ImageMessage("Unable to Free Local Memory");
return(image);
}
}
return(image); // Return image pointer
}
/*
-------------------------------------------------------------------------------
IMAGE *DuplicateImage(IMAGE *image)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This procedure allows the user to duplicate a image structure
in the PC memory space. There is no limit on the number of memory FBs
that can be created as long as there is memory space available. The orginal
image is not destroyed.
Parameters:
image - pointer to source image
Return value: